﻿using System;
using System.Text;

namespace ExpressesClass.BestApi.Utils.jsonUtil
{
    class BufferErrorListener : JSONErrorListener
    {

        protected StringBuilder buffer;
        private String input;

        public BufferErrorListener(StringBuilder buffer)
        {
            this.buffer = buffer;
        }

        public BufferErrorListener()
        {
            new BufferErrorListener(new StringBuilder());
        }

        public void end()
        {
            
        }

        public void error(string message, int column)
        {
            buffer.Append("expected ");
            buffer.Append(message);
            buffer.Append(" at column ");
            buffer.Append(column);
            buffer.Append("\n");
            buffer.Append(input);
            buffer.Append("\n");
            indent(column - 1, buffer);
            buffer.Append("^");
        }

        private void indent(int n, StringBuilder ret)
        {
            for (int i = 0; i < n; ++i)
            {
                ret.Append(' ');
            }
        }

        public void start(string text)
        {
            this.input = text;
            buffer.Length = 0;
        }
    }
}
